﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Diagnostics;
using System.ComponentModel;
using DAO;
using Microsoft.Win32;

namespace TAM_SPORT
{
    class ShareMethod
    {
        private bool showError = false;
        /// <summary>
        /// A property to show or hide error messages 
        /// (default = false)
        /// </summary>
        public bool ShowError
        {
            get { return showError; }
            set { showError = value; }
        }

        private static string subKey = "SOFTWARE\\" + "FGM".ToUpper();
        public static string SubKey
        {
            get { return subKey; }
            set { subKey = value; }
        }

        private static RegistryKey baseRegistryKey = Registry.LocalMachine;

        public static RegistryKey BaseRegistryKey
        {
            get { return baseRegistryKey; }
            set { baseRegistryKey = value; }
        }

        public static string Read(string KeyName)
        {
            RegistryKey rk = baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(subKey);
            if (sk1 == null)
            {
                return null;
            }
            else
            {
                try
                {
                    return (string)sk1.GetValue(KeyName.ToUpper());
                }
                catch (Exception e)
                {
                    Console.Write(e.ToString());
                    return null;
                }
            }
        }


        public static bool Write(string KeyName, object Value)
        {
            try
            {
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.CreateSubKey(subKey);
                sk1.SetValue(KeyName.ToUpper(), Value);

                return true;
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                return false;
            }
        }




        public static bool DeleteKey(string KeyName)
        {
            try
            {
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.CreateSubKey(subKey);
                if (sk1 == null)
                    return true;
                else
                    sk1.DeleteValue(KeyName);

                return true;
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                return false;
            }
        }


        public static bool DeleteSubKeyTree()
        {
            try
            {
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);
                if (sk1 != null)
                    rk.DeleteSubKeyTree(subKey);

                return true;
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                return false;
            }
        }

        public static int SubKeyCount()
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);
                if (sk1 != null)
                    return sk1.SubKeyCount;
                else
                    return 0;
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                return 0;
            }
        }

        public static int ValueCount()
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);
                // If the RegistryKey exists...
                if (sk1 != null)
                    return sk1.ValueCount;
                else
                    return 0;
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                return 0;
            }
        }





        public static string getTheNextId(string pre, int no)
        {
            string strId = pre;
            if (no == -1)
            {
                return null;
            }
            if (no < 10)
            {
                strId += "000" + no;
                return strId;
            }
            else if (no >= 10 && no < 100)
            {
                strId += "00" + no;
                return strId;
            }
            else if (no >= 100 && no < 1000)
            {
                strId += "0" + no;
                return strId;
            }
            else if (no >= 1000 && no < 10000)
            {
                strId += no;
                return strId;
            }
            else
                return null;
        }
        public static string getTheNextIdIncludeSysDate(string pre, int no)
        {

            string strId = pre + getSysDate().ToString("yyMMdd");
            if (no == -1)
            {
                return null;
            }
            if (no < 10)
            {
                strId += "000" + no;
                return strId;
            }
            else if (no >= 10 && no < 100)
            {
                strId += "00" + no;
                return strId;
            }
            else if (no >= 100 && no < 1000)
            {
                strId += "0" + no;
                return strId;
            }
            else if (no >= 1000 && no < 10000)
            {
                strId += no;
                return strId;
            }
            else
                return null;
        }
        public static DateTime getSysDate()
        {
            return SqlHelper.getSysDate();
        }
        public static string readFileTxtResource(string key)
        {
            StreamReader file = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + @"\ApplicationResources.txt");
            string input = null;
            string id;
            string value;
            int index;
            while ((input = file.ReadLine()) != null)
            {
                if (!input.Contains("************"))
                {
                    index = input.IndexOf("=");
                    if (index > -1)
                    {
                        id = input.Substring(0, index).Trim();
                        value = input.Substring(index + 1).Trim();
                        if (id != null && id.Equals(key))
                        {
                            return value;
                        }
                    }
                }
            }
            file.Close();
            return null;
        }
        private static string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }
        private static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
        public static string GetPassword()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomString(4, true));
            builder.Append(RandomNumber(1000, 9999));
            builder.Append(RandomString(2, false));
            return builder.ToString();
        }

        public static bool checkEmail(string email)
        {
            string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_]
                    [a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
            System.Text.RegularExpressions.Match match =
                Regex.Match(email, pattern, RegexOptions.IgnoreCase);
            if (match.Success)
                return true;
            else
                return false;
        }

        public static bool IsNumber(string text)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(text);
        }
        public static bool IsInteger(string text)
        {
            try
            {
                return IsNumber(text);
            }
            catch
            {
                return false;
            }
        }
        public static int AgeInYears(DateTime dob)
        {
            int ageInYrs = DateTime.Now.Year - dob.Year;
            if (DateTime.Now.Month < dob.Month ||
                (DateTime.Now.Month == dob.Month && DateTime.Now.Day < dob.Day))
                ageInYrs--;
            return ageInYrs;
        }
        public static int MontInYear(DateTime dob)
        {
            int ageInYrs = DateTime.Now.Year - dob.Year;
            if (DateTime.Now.Month < dob.Month ||
                (DateTime.Now.Month == dob.Month && DateTime.Now.Day < dob.Day))
                ageInYrs--;
            int month = DateTime.Now.Month - dob.Month;
            return ageInYrs * 12 + month;
        }
        public static bool compareDate(DateTime date1, DateTime date2)
        {
            if (date1.Year < date2.Year)
            {
                return false;
            }
            else
            {
                if (date1.Month < date2.Month)
                {
                    return false;
                }
                else
                {
                    if (date1.Day < date2.Day)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        public static bool emptyFolder(DirectoryInfo directoryInfo)
        {
            if (directoryInfo.Exists)
            {
                foreach (FileInfo file in directoryInfo.GetFiles())
                {
                    try
                    {
                        file.Delete();
                    }
                    catch
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                createFolder(directoryInfo.FullName);
                return true;
            }
        }
        public static bool copyFile(string sourcePath, string targetPath, string fileName)
        {
            try
            {
                string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                string destFile = System.IO.Path.Combine(targetPath, fileName);
                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }
                System.IO.File.Copy(sourceFile, destFile, true);
                if (System.IO.Directory.Exists(sourcePath))
                {
                    string[] files = System.IO.Directory.GetFiles(sourcePath);
                    foreach (string s in files)
                    {
                        fileName = System.IO.Path.GetFileName(s);
                        destFile = System.IO.Path.Combine(targetPath, fileName);
                        System.IO.File.Copy(s, destFile, true);
                    }
                }
                else
                {
                    Console.WriteLine("Source path does not exist!");
                    return false;
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
        
        public static bool createFolder(string activeDir)
        {
            try
            {
                if (!Directory.Exists(activeDir))
                {
                    Directory.CreateDirectory(activeDir);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
